package com.example.springbott.service;

import com.example.springbott.bean.MarketCoupon;
import com.example.springbott.bean.MarketGoods;
import com.example.springbott.bean.MarketTopic;
import com.example.springbott.bean.MarketTopicExample;
import com.example.springbott.common.CommonData;
import com.example.springbott.mapper.MarketGoodsMapper;
import com.example.springbott.mapper.MarketTopicMapper;
import com.example.springbott.utils.StringUtils;
import com.example.springbott.vo.ReadTopicVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 付常莹
 * @description: TODO
 * @date 2022/11/20 21:19
 */
@Service
public class MarketTopicServiceIml implements MarketTopicService {
    @Resource
    MarketTopicMapper marketTopicMapper;

    @Resource
    MarketGoodsMapper marketGoodsMapper;

    /**
     * 该方法用于查找专题
     * @param page
     * @param limit
     * @param title
     * @param subtitle
     * @param sort
     * @param order
     * @return
     */
    @Override
    public CommonData<MarketTopic> list(Integer page, Integer limit, String title, String subtitle, String sort, String order) {
        PageHelper.startPage(page,limit);
        MarketTopicExample topicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = topicExample.createCriteria();
        topicExample.setOrderByClause(sort+" "+order);
        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike(title);
        }
        if (!StringUtils.isEmpty(subtitle)) {
            criteria.andSubtitleLike(subtitle);
        }
        List<MarketTopic> list = marketTopicMapper.selectByExample(topicExample);
        PageInfo<MarketTopic> pageInfo = new PageInfo<MarketTopic>(list);
        int page1 = pageInfo.getPageNum();
        int pages = pageInfo.getPages();
        int limit1 = pageInfo.getPageSize();
        long total = pageInfo.getTotal();
        List<MarketTopic> list1 = pageInfo.getList();
        CommonData<MarketTopic> data = new CommonData<>(page1, pages,(int) total, limit1,  list1);
        return data;
    }


    /**
     * 编辑专题信息
     * @param marketTopic
     * @return
     */
    @Override
    public MarketTopic update(MarketTopic marketTopic) {
        int i = marketTopicMapper.updateByPrimaryKey(marketTopic);
        MarketTopic data = marketTopicMapper.selectByPrimaryKey(marketTopic.getId());
        return data;
    }



    /**
     * 编辑专题之前的信息回显，返回应该是一个对象
     * @param id
     * @return
     */
    @Override
    public ReadTopicVo read(Integer id) {
        MarketTopic data = marketTopicMapper.selectByPrimaryKey(id);
        List<MarketGoods> marketGoods = new ArrayList<>();
        Integer[] goods = data.getGoods();
        for (Integer good : goods) {
            MarketGoods marketGoods1 = marketGoodsMapper.selectByPrimaryKey(good);
            marketGoods.add(marketGoods1);
        }
        ReadTopicVo readTopicVo = new ReadTopicVo();
        readTopicVo.setGoodsList(marketGoods);
        readTopicVo.setTopic(data);
        return readTopicVo;
    }


    /**
     * 删除专题
     * @param marketTopic
     */
    @Override
    public void delete(MarketTopic marketTopic) {
        marketTopicMapper.deleteByPrimaryKey(marketTopic.getId());
        return;
    }


    /**
     * 新增专题
     * @param marketTopic
     * @return
     */

    @Override
    public MarketTopic createTopic(MarketTopic marketTopic) {
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        marketTopic.setAddTime(date);
        marketTopic.setUpdateTime(date);
        marketTopicMapper.insertSelective(marketTopic);
        MarketTopic marketTopic1 = marketTopicMapper.selectByPrimaryKey(marketTopic.getId());
        return marketTopic1;
    }


    /**
     * 批量删除专题
     * @param map
     */
    @Override
    public void batchDelete(Map<String, Integer[]> map) {
        Integer[] ids = map.get("ids");
        if (ids != null) {
            for (Integer id : ids) {
                marketTopicMapper.deleteByPrimaryKey(id);
            }

        }
    }
}
